

$(function(){
    //判断当前是否有用户登录

    if($.cookie("spInfor")){
        var currentUser = JSON.parse($.cookie("spInfor")).username;
        //console.log(currentUser);
    }
    if(currentUser){
       //获取到当前用户的信息
        $(".overTop_left>#login").remove();
        $(".overTop_left>#regist").remove();
        var userSpan = document.createElement("span");
        var exitA = document.createElement("a");
        userSpan.innerHTML = currentUser;
        exitA.innerHTML = "退出";
        $(".overTop_left").append($(userSpan)).append($(exitA));

        $(exitA).on("click",function(){
            $.removeCookie("spInfor");
            window.location.reload();
        })
        //将购物车中的寄存商品转入数据库
        if($.cookie("goodsses")){
            console.log(JSON.parse($.cookie("goodsses")));
            var cookieLen = 0;
            var trans = {
                "url":"http://127.0.0.1/hz_1706/spUser/version1/spcar.php",
                "dataType":"json",
                data:{},
                "success":function(res){
                    cookieLen++;
                    if(cookieLen==JSON.parse($.cookie("goodsses")).length){
                        $.removeCookie("goodsses");
                    }
                }
            }
            trans.data.phonenumber = currentUser;
            JSON.parse($.cookie("goodsses")).forEach(function(ele,index){
                trans.data.serial = ele.serial;
                trans.data.sum = ele.sum;
                $.ajax(trans);
            })
        }



    }else{
        //未获取到用户信息
    }


    //----------------------封装一个方法----便捷更新nav-----------------------------------------------
    function currNav(origin, target, imgAlt){
        origin.forEach(function(ele,index){
            var temp_div = document.createElement("div");
            target.append(temp_div);
            temp_div.innerHTML = "<a href='#'><img src='"+ele.imgSrc+"' alt='"+imgAlt+"'></a><p><a href='#'>"+ele.tabTitle+"</a></p><p><em>价格：</em><b>"+ele.price+"</b></p>"
        })
    }

    var detailData = {
        url:"../json/mainCont_data.json",
        success:function(data){
            console.log(data.nav.life);
            currNav(data.nav.life,$("#subNav>li").eq(0).find("section.devide"),"life");
            currNav(data.nav.digital,$("#subNav>li").eq(1).find("section.devide"),"life");
            currNav(data.nav.fitness,$("#subNav>li").eq(2).find("section.devide"),"life");
            currNav(data.nav.clothing,$("#subNav>li").eq(3).find("section.devide"),"life");
            currNav(data.nav.beauty,$("#subNav>li").eq(4).find("section.devide"),"life");
            currNav(data.nav.health,$("#subNav>li").eq(5).find("section.devide"),"life");
            currNav(data.nav.treasure,$("#subNav>li").eq(6).find("section.devide"),"life");


        }
    }
    $.ajax(detailData);
    var oriNum = 1;
    $("#plus").on("click",function(){
        $("#numsp")[0].value =parseInt($("#numsp")[0].value)+1;
        if($("#numsp")[0].value>999){
            $("#numsp")[0].value=999;
        }
    })
    $("#subtra").on("click",function(){
        $("#numsp")[0].value =parseInt($("#numsp")[0].value)-1;
        if($("#numsp")[0].value<1){
            $("#numsp")[0].value=1;
        }

    })
    $("#numsp").on("focus",function(){
        oriNum = this.value;
    })
    $("#numsp").on("blur",function(){
        if(isNaN(parseInt(this.value))){
            this.value = oriNum;
        }else{
            this.value=parseInt(this.value);
        }
        if(this.value<1){
           this.value=1;
        }
        if(this.value>999){
            this.value=999;
        }
    })

})
